/*
 * Copyright (C) Francisco Morero Peyrona. All rights reserved.
 *
 * This software is published under the terms of Open Source
 * License version 1.1, a copy of which has been included with this
 * distribution in the License.txt file.
 */

package jdbcmanager.swing;

import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;

/**
 * A basic abstract implementation of AbstractAction which contains common methos to 
 * make things more clear when subclassing. 
 * 
 * @author Francisco Morero Peyrona
 */
public abstract class JAbstractAction extends AbstractAction
{
   private EventListenerList listeners;
   
   //-------------------------------------------------------------------------//
   
   public String    getName()              { return (String)    getValue( Action.NAME );               }
   public String    getActionCommandKey()  { return (String)    getValue( Action.ACTION_COMMAND_KEY ); }
   public String    getShortDescription()  { return (String)    getValue( Action.SHORT_DESCRIPTION );  }
   public String    getLongDescription()   { return (String)    getValue( Action.LONG_DESCRIPTION );   }
   public ImageIcon getSmallIcon()         { return (ImageIcon) getValue( Action.SMALL_ICON );         }
   public Integer   getMnemonicKey()       { return (Integer)   getValue( Action.MNEMONIC_KEY );       }
   public KeyStroke getKeyStroke()         { return (KeyStroke) getValue( Action.ACCELERATOR_KEY );    }
   
   public void setName( String s )              { putValue( Action.NAME, s );                        }
   public void setActionCommandKey( String s )  { putValue( Action.ACTION_COMMAND_KEY, s );          }
   public void setShortDescription( String s )  { putValue( Action.SHORT_DESCRIPTION, s );           }
   public void setLongDescription( String s )   { putValue( Action.LONG_DESCRIPTION, s );            }
   public void setSmallIcon( ImageIcon i )      { putValue( Action.SMALL_ICON, i );                  }
   public void setMnemonicKey( char c )         { putValue( Action.MNEMONIC_KEY, new Integer( c ) ); }
   public void setKeyStroke( KeyStroke k )      { putValue( Action.ACCELERATOR_KEY, k );             }
   

   public void addActionListener( ActionListener l )
   {
      if( this.listeners == null )
         this.listeners = new EventListenerList();
      
      this.listeners.add( ActionListener.class, l );
   }

   public void removeActionListener( ActionListener l )
   {
      if( this.listeners == null )
         return;
      
      this.listeners.remove( ActionListener.class, l );
   }

   /**
    * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
    */
   public void actionPerformed( ActionEvent evt )
   {
      if( this.listeners != null )
      {
         Object[] aListener = this.listeners.getListenerList();

         // Recreate the ActionEvent and stuff the value of the ACTION_COMMAND_KEY
         ActionEvent e = new ActionEvent( evt.getSource(), evt.getID(), getActionCommandKey() );

         for( int n = 0; n <= aListener.length - 2; n += 2 )
             ((ActionListener) aListener[n + 1]).actionPerformed( e );
      }
   }
}
